﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.Page
{
    public partial class XuLyDangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Form["userPostAjax"] == null&&Request.Form["emailPostAjax"]==null)
                {
                    Data.UserInfo user = new Data.UserInfo();
                    user.Username = Request.Form["user"];
                    user.Address = Request.Form["addressto"];
                    user.Provenumber = "";
                    user.Phone = Request.Form["phoneto"];
                    user.Email = Request.Form["email"];
                    user.AddressTo = Request.Form["addressto"];
                    user.FullNameTo = Request.Form["fullnameto"];
                    user.Fullname = Request.Form["fullname"];
                    user.MailTo = Request.Form["emailto"];
                    user.PhoneTo = Request.Form["phoneto"];
                    user.University = Request.Form["university"];
                    user.Provenumber = "123";

                    string maxacnhan = Request.Form["maxacnhan"];

                    if (maxacnhan != Shopping.SessionCaptcha.Inst.Session)
                    {
                        Response.Write("<script>alert('Mã xác nhận không chính xác '); window.location.href='/Register.aspx';</script>");
                    }
                    else
                    {
                        user.isAdmin = "0";
                        if (!Data.Mysession.isLogin)
                        {
                            user.Class = "";

                            user.Status = "1";
                            user.Pass = Request.Form["pass"];
                            Business.UserService.User_Insert(user);

                            Data.Mysession.Session = Business.UserService.User_GetByUserPass(user.Username, user.Pass)[0];
                            Response.Write("<script>alert('Bạn đã đăng ký thành công '); window.location.href='/index.aspx';</script>");
                        }
                        else
                        {
                            user.Class = "";

                            user.Status = "1";
                            user.Pass = Data.Mysession.Session.Pass;
                            user.UId = Data.Mysession.Session.UId;
                            Business.UserService.User_Update(user);
                            Response.Write("<script>alert('Bạn đã cập nhật thông tin thành công '); window.location.href='/index.aspx';</script>");
                        }
                    }
                }
                else 
                {
                    if (Request.Form["userPostAjax"] != null && Request.Form["emailPostAjax"] == null)
                    {
                        string userPostAjax = Request.Form["userPostAjax"];
                        if (Business.UserService.User_GetByAll().FindAll(u => u.Username == userPostAjax).Count > 0)
                        {
                            Response.Write("0");
                        }
                        else
                        {
                            Response.Write("1");
                        }
                    }
                    else
                    {
                            
                    }
                }
            }
        }
    }
}